from scipy.stats import f,t
import numpy as np
import math
def equivalence_paired_t_test(alpha, std_dev, equivalence_limit, expected_difference, power):
    """
    Calculate sample size for equivalence paired t-test.

    Parameters:
    - alpha: significance level (e.g., 0.025 for 2.5%)
    - std_dev: standard deviation of the differences (σ)
    - equivalence_limit: allowed equivalence limit difference (Δ_0)
    - expected_difference: expected mean difference (Δ_1)
    - power: desired power (e.g., 90 for 90%)

    Returns:
    - Sample size required for the t-test
    """
    effect_size = abs(expected_difference - equivalence_limit) / std_dev
    n = 2  # Initial sample size

    while True:
        df = n - 1
        ncp = math.sqrt(n) * effect_size
        t_crit = t.ppf(1 - alpha, df)
        achieved_power = 1 - t.cdf(t_crit, df, ncp)

        if achieved_power >= power / 100:
            return math.ceil(n)

        n += 0.01


# Example:
equivalence_t_test_example = equivalence_paired_t_test(
    alpha=0.025,
    std_dev=30,
    equivalence_limit=10,
    expected_difference=0,
    power=90
)
print(f"Equivalence Paired t-Test Sample Size: {equivalence_t_test_example}")